﻿using System;
using System.Net.Mail;

namespace DotNetNexusSpam.SpamFilter
{
    class SpecialCharFilter : FilterBase
    {
        public SpecialCharFilter(CDOMessageWrapper email, string startupPath)
            : base(email, startupPath)
        {
            base.Name = "SpecialChars";
        }

        public override void Fire()
        {
            base.ActionToTake = FilterAction.PROCESS_NORMALLY;
            base.IsSpam = false;

            int specialCharsSum = 0, wordsSum = 0;
            string[] body = Email.CDOMessage.TextBody.Split(new char[] { ' ' });
            wordsSum = body.Length;
            foreach (string word in body) {
                if (word.Contains("~") ||
                    word.Contains("?") ||
                    word.Contains("*") ||
                    word.Contains("|") ||
                    word.Contains("_") ||
                    word.Contains("!") ||
                    word.Contains("#") ||
                    word.Contains("+") 
                    )
                {
                    ++specialCharsSum;
                }
            }

            if ((specialCharsSum / wordsSum) * 100 > 4) {
                base.ActionToTake = FilterAction.ADD_TAG_TO_SUBJECT;
                base.IsSpam = true; 
            } 
        }

        public override void LoadConfiguration()
        {
            // nothing to load
        }
    }
}